/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fujitsu.nanjing.tieba.base.BaseController;
import com.fujitsu.nanjing.tieba.ibator.CoreTopic;
import com.fujitsu.nanjing.tieba.service.TopicService;

/**
 * NewTopicsController.java
 * 
 * @author Administrator
 */
@Controller
public class NewTopicsController extends BaseController {
	@Resource
	private TopicService topicService;
	@RequestMapping(value = "/newTopics", method = RequestMethod.GET)
	public String recentTopics() {
		return redirect("/newTopics/pages/1");

	}
	@RequestMapping(value = "/newTopics/pages/{pageIndex}", method = RequestMethod.GET)
	public String recentTopics(@PathVariable String pageIndex, Model model,
			HttpServletRequest request) {
		pageIndex = (pageIndex == null ? "0" : pageIndex);
		model.addAttribute("pageIndex", pageIndex);
		Date lastAccess = (Date) request.getSession().getAttribute(LASTACCESS);
		List<CoreTopic> topics = topicService.findNewTopics(lastAccess,
				Integer.parseInt(pageIndex));
		model.addAttribute("topics", topics);
		model.addAttribute("pageCount",
				topicService.findPageCountByDate(lastAccess));
		return "newTopics/newTopics";
	}

}
